var app = getApp();
var WxParse = require('../../lib/wxParse/wxParse.js');
var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    id: 0,
    itemId: 0,
    index: 0,
    time: 30 * 60 * 60 * 1000,
    purchase: {},
    purchasesStr: '',
    imgs: [],
    videoContext:null,
    video_data: '',
    fullScreen: false
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    // console.log(options)
    that.setData({
      id: options.id,
      itemId: options.itemId,
      index: options.index,
      purchasesStr: wx.getStorageSync('purchasesStr')
    });

    util.request(api.PurchaseExtraDetail, {
      id: that.data.id,
      itemId: that.data.itemId
    }).then(function (res) {
      if (res.errno === 0) {
        // console.log(res.data)
        let purchase = res.data.purchase;
        purchase.publishDt = util.formatTime2(new Date(purchase.publishDt), "Y/M/D");
        purchase.supplyPrice = util.toDecimal(purchase.supplyPrice);
        purchase.dueTime = parseInt((new Date(purchase.dueTime).getTime() - new Date().getTime()));
        purchase.minTime = 10 * 60;
        let herbListImage = purchase.herbListImage;
        let imgs = '';
        if (herbListImage) {
          imgs = herbListImage.split(',')
        }

        that.setData({
          purchase: purchase,
          imgs: imgs
        });
      }
    });
  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  quote: function () {
    wx.setStorageSync('purchaseListStr', this.data.purchasesStr);
    wx.navigateTo({
      url: `/pages/quote/quote`
    })
  },
  //拨打电话
  phoneCall: function () {
    wx.makePhoneCall({
      phoneNumber: this.data.purchase.contactTel
    });
  },
  playVideo(){
    this.setData({
      video_data: this.data.purchase.herbVideo
    });
    this.play();
  },
  play(e) {
    //执行全屏方法  
    this.videoContext = wx.createVideoContext('myvideo', this);
    this.videoContext.requestFullScreen();
    // this.videoContext.play();
    this.setData({
      fullScreen: true
    })
  },
  /**关闭视屏 */
  closeVideo() {
    //执行退出全屏方法
    this.videoContext.exitFullScreen();
  },
  /**视屏进入、退出全屏 */
  fullScreen(e) {
    var isFull = e.detail.fullScreen;
    //视屏全屏时显示加载video，非全屏时，不显示加载video
    this.setData({
      fullScreen: isFull
    })
  },
  previewImage(e){
    if(e.currentTarget.dataset.src){
      wx.previewImage({
        current: e.currentTarget.id, // 当前显示图片的http链接
        urls: [e.currentTarget.dataset.src]// 需要预览的图片http链接列表
      })
    }
  }
})